﻿using mscook.core;
using mscook.core.components;
using mscook.core.domain;
using mscook.core.repositories;
using mscook.core.services;
using mscook.mvc.core;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace mscook.mvc.Areas.weblogin.Controllers
{
    public class FeatureController : BaseController
    {
        //
        // GET: /weblogin/Feature/
        private IFeatureRepository iFeatureRepository;
        private ILoginUserService iLoginUserService;
        private IImageSettingFactory iImageSettingFactory;
        private readonly string SectionName = "Feature";
        public FeatureController(IFeatureRepository iFeatureRepository, IImageSettingFactory iImageSettingFactory, ILoginUserService iLoginUserService)
            : base(iLoginUserService)
        {
            this.iLoginUserService = iLoginUserService;
            this.iFeatureRepository = iFeatureRepository;
            this.iImageSettingFactory = iImageSettingFactory;
        }

        public static Encoding encoder = Encoding.UTF8;
        public ActionResult Index(string nowPageNum, string per = "")
        {
            PageBreakHelper pageHelper = new PageBreakHelper();
            pageHelper.Init(nowPageNum, per);
            long total = 0;
            IList<Feature> slides = iFeatureRepository.SelectPageList(pageHelper.Start, pageHelper.PerValue, out total);
            pageHelper.SetViewDataKeys(ViewData, ViewBag, total);

            InitNavBar();

            ViewData["nowPageNum"] = nowPageNum;
            ViewData["per"] = per;
            return View(slides);
        }
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult add()
        {
            InitNavBar();
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult add(Feature feature)
        {
            if (ModelState.IsValid)
            {
                SaveUploadImage(feature);
                iFeatureRepository.SaveOrUpdate(feature);
                return RedirectToIndex();
            }
            return View();
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Edit(string id)
        {
            Feature feature = iFeatureRepository.Get(id);
            if (feature != null)
            {
                InitNavBar();
                return View(feature);
            }
            //If result is null, redirect to index
            return RedirectToIndex();
        }


        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult Edit(string id, string image)
        {
            Feature feature = iFeatureRepository.Get(id);
            if (ModelState.IsValid)
            {
                UpdateModel(feature);
                if (SaveUploadImage(feature))
                {
                    iFeatureRepository.SaveOrUpdate(feature);
                    return RedirectToIndex();
                }
            }
            return View();
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Delete(string id)
        {
            iFeatureRepository.Delete(id);
            return RedirectToIndex();
        }

        public bool SaveUploadImage(Feature feature)
        {
            if (Request.Files.Count > 0)
            {
                HttpPostedFileBase file = Request.Files.Get(0);
                if (file.ContentLength != 0)
                {
                    feature.ImagePath = iImageSettingFactory.CreateBatchSizedImage(SectionName, file.FileName, file.InputStream);
                }
                return true;
            }
            return false;
        }
       

    }
}
